﻿@using MvcJqGrid
@{
    ViewBag.Title = "Search Product";
}

@section Scripts
{
    <script type="text/javascript">

        function EditDeleteProduct(cellvalue, options, rowobject) {
            return '<a onclick="return EditProduct(' + options.rowId + ')" href="#"><div class="ui-icon ui-icon-pencil" style="display:inline-block"></div></a>' +
                   '<a onclick="return DeleteProduct(' + options.rowId + ')" href="#"><div class="ui-icon ui-icon-trash" style="display:inline-block"></div></a>';

        }

        function EditProduct(id) {
            var url = '@Url.Action("ProductDetails", "Inventory")/?id=' + id;          
            $("#dialogEdit").dialog({
                autoOpen: true,
                resizable: false,
                height: 300,
                show: { effect: 'drop', direction: "up" },
                width: 450,
                modal: true,
                draggable: true,
                open: function (event, ui) {
                    $(this).load(url);
                }, close: function (event, ui) {
                    $(this).dialog('close');
                }
            });
        }

        function DeleteProduct(id) {
            var url = '@Url.Action("DeleteProduct", "Inventory")/?id=' + id;
            $("#dialogConfirm").dialog({
                autoOpen: true,
                resizable: false,
                height: 170,
                show: { effect: 'drop', direction: "up" },
                width: 350,
                modal: true,
                draggable: true,
                buttons: {
                    "OK": function () {
                        $(this).dialog("close");
                        window.location = url;
                    },
                    "Cancel": function () {
                        $(this).dialog("close");
                    }
                }
            });
        }

        function reloadGrid(refresh) {
            if (refresh == true) {
                //Set initial values of your filters here
                $("#txtId").val("");
                $("#txtEmployeeName").val("");

                //reload grid's page 1 instead of current page
                $("#jq-grid").trigger("reloadGrid", { page: 1 });
            }
            else {
                var text = $("#txtName").val();
                var postdata = $("#ModelGrid").jqGrid('getGridParam', 'postData');
                alert("testing");
                alert(postdata);
                $.extend(postdata, { filters: '', searchField: 'Name', searchOper: 'bw', searchString: text });
                $("#ModelGrid").jqGrid('setGridParam', { search: text.length > 0, postData: postdata });
                $("#ModelGrid").trigger("reloadGrid", [{ page: 1 }]);
            }
        }
</script>
}

<div id="dialogEdit" style="display: none">
</div>

<div id="dialogConfirm" title="Delete Product" style="display: none">
    <p>
        <span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span>
        Are you sure to delete ?
    </p>
</div>

<a href="@Url.Action("ProductDetails", "Inventory")">Create Product</a>

<div style="width: 670px;" class="ui-jqgrid ui-widget ui-widget-content ui-corner-all">
    <div class="ui-jqgrid-titlebar ui-widget-header ui-corner-top ui-helper-clearfix">
        Search Filters
    </div>
    <div style="clear: both;">
    </div>
    <div style="float: left; width: 200px; padding: 5px 5px 5px 5px">
        Name:
    </div>
    <div style="float: left; padding: 5px 5px 5px 5px">
        <input type="text" id="txtName" value="" class="inp-form" />
    </div>
    <div style="clear: both;">
    </div>
    <div style="float: left; width: 200px; padding: 5px 5px 5px 5px">
        Description:
    </div>
    <div style="float: left; padding: 5px 5px 5px 5px">
        <input type="text" id="txtDescripton" value="" class="inp-form" />
    </div>
    <div style="clear: both;">
    </div>
    <div style="float: left;">
        <input type="button" id="lnkSearch" class="form-submit" value="Search" onclick="reloadGrid(false);" />
    </div>
    <div style="clear: both; width: 100px;">
    </div>
</div>
<br />
<br />
@(Html.Grid("ModelGrid")
    .SetRequestType(MvcJqGrid.Enums.RequestType.Post)
    .SetJsonReader(new MvcJqGrid.DataReaders.JsonReader { Id = "ID", RepeatItems = false })
    .SetAutoWidth(true)
    .SetRowNumbers(true)
    .SetShowAllSortIcons(true)
    .SetToolbar(true)
    .SetToolbarPosition(ToolbarPosition.Bottom)
    .SetFooterRow(true)
    .SetHeaderTitles(true)
    .SetCaption("Product Model")
    .AddColumn(new Column("Name").SetLabel("Product Name"))
    .AddColumn(new Column("Description"))
    .AddColumn(new Column("ExpiryDate").SetFormatter(Formatters.Date).SetAlign(Align.Right))
    .AddColumn(new Column("PurchaseDate").SetFormatter(Formatters.Date).SetAlign(Align.Right))
    .AddColumn(new Column("PurchaseRate").SetAlign(Align.Right))
    .AddColumn(new Column("MenuFacturingDate").SetFormatter(Formatters.Date).SetAlign(Align.Right))
    .AddColumn(new Column("OpeningQty").SetAlign(Align.Right))
    .AddColumn(new Column("OpeningValue").SetAlign(Align.Right))
    .AddColumn(new Column("SaleRate").SetAlign(Align.Right))
    //.AddColumn(new Column("CategoryId"))
    //.AddColumn(new Column("ModelId"))
    //.AddColumn(new Column("MenufacturerId"))
    //.AddColumn(new Column("CreateBy").SetLabel("Created By"))
    //.AddColumn(new Column("CreatedDate").SetAlign(Align.Right).SetFormatter(Formatters.Date))
    //.AddColumn(new Column("UpdatedBy").SetLabel("Updated By"))
    //.AddColumn(new Column("UpdatedDate").SetAlign(Align.Right).SetFormatter(Formatters.Date))
    .AddColumn(new Column("Action").SetSearch(false).SetCustomFormatter("EditDeleteProduct")
                                    .SetWidth(50)
                                    .SetAlign(Align.Right).SetSortable(false))
    .SetUrl(Url.Action("ProductGrid", "Inventory", new { @name = "abc", @description = "des" }))
    .SetAutoWidth(true)
    .SetRowNum(10)
    .SetRowList(new[] { 10, 15, 20, 50 })
    .SetViewRecords(true)
    .SetPager("pager")
    )
